package com.itheima.module.web.controller.param;

import com.itheima.module.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;


@RequestMapping("/param")

/**
 * 如果我们的类上出现了ResponseBody 注解 表示 该controller的所有方法都带有 ResponseBody 注解
 */
/*@Controller
@ResponseBody*/
@RestController
public class ParamController {
    /**
     * 登陆场景
     * @return
     */
    @RequestMapping("/test1")
    public String test1(HttpServletRequest request){

        //接受到用户的请求参数把
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username:"+username);
        System.out.println("password:"+password);

        return "success";
    }

    /**
     * 参数 就是需要啥嘛 就写啥嘛 就行了
     * @param username  跟参数名保持一致
     * @param password 跟参数名保持一致
     * @return
     */
    @RequestMapping("/test2")
    public String test2(String username,String password){
        System.out.println("username:"+username);
        System.out.println("password:"+password);

        return "success";
    }

    /**
     *
     * 一旦加上 requestParam注解 附作用 就是该参数必须的
     *
     * @param username
     * @param password
     * @param age  注意如果是int double float ...基本类型 那么必须存在 就会报错..
     * @return
     */
    @RequestMapping("/test3")
    public String test3(@RequestParam("username") String username,
                        @RequestParam("password")String password,
                        @RequestParam("age") Integer age
    ){
        System.out.println("username:"+username);
        System.out.println("password:"+password);
        System.out.println("age:"+age);

        return "success";
    }

    /**
     * 有一些参数 可选的 有也行 没有也行
     *                                      required属性 标识是否必须存在 默认是true
     *   @RequestParam(value="phoneNum",required = false)
     * @param username
     * @param password
     * @param phoneNum
     * @return
     */
    @RequestMapping("/test4")
    public String test3(@RequestParam("username") String username,
                        @RequestParam("password")String password,
                        @RequestParam(value="phoneNum",required = false) String phoneNum
    ){
        System.out.println("username:"+username);
        System.out.println("password:"+password);
        System.out.println("phoneNum:"+phoneNum);

        return "success";
    }

    /**
     *                                  默认值 不传参我用默认值 传参 我听你
     * @RequestParam(value = "pageNum",defaultValue = "1")
     *
     * 分页场景
     *  pageNum 分页的页码 第几页的意思
     *
     *  不传也行 我给默认值
     *
     * @return
     */
    @RequestMapping("/test5")
    public String test5(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum) {
        System.out.println("pageNum:"+pageNum);


        return "success";
    }

    /**
     * 常见的类型
     */
    @RequestMapping("/test6")
    public String test6(
            @RequestParam(value = "s")String s,
            @RequestParam(value = "i")int i,
            @RequestParam(value = "d")double d,
            @RequestParam(value = "f")float f,
            @RequestParam(value = "b")boolean b

    ) {
        System.out.println(s);
        System.out.println(i);
        System.out.println(d);
        System.out.println(f);
        System.out.println(b);


        return "success";
    }

    /**
     * 注册用户
     * 用户名
     * 密码
     * 爱好 多个值~~~
     *
     */
    @RequestMapping("/test7")
    public String test7(
            @RequestParam(value = "username")String username,
            @RequestParam(value = "password")String password,
            @RequestParam(value = "hobbies")String[] hobbies
    ) {
        System.out.println(username);
        System.out.println(password);
        System.out.println(Arrays.toString(hobbies));


        return "success";
    }

    /**
     *  处理 默认不会处理类型
     *  2020-12-23 格式 无法处理
     *  2020/12/23 可以自动处理
     *
     *
     * @param username
     * @param password
     * @param birthday
     * @return
     */
    @RequestMapping("/test8")
    public String test8(
            @RequestParam(value = "username")String username,
            @RequestParam(value = "password")String password,
            @RequestParam(value = "birthday")Date birthday
            ) {
        System.out.println(username);
        System.out.println(password);
        System.out.println(birthday);

        return "success";
    }

    /**
     * 模拟注册场景
     * 很多参数
     *
     *
     * @return
     */
    @RequestMapping("/test9")
    public String test9(
            @RequestParam(value = "username")String username,
            @RequestParam(value = "password")String password,
            @RequestParam(value = "age")int age,
            @RequestParam(value = "phone")String phone,
            @RequestParam(value = "email")String email,
            @RequestParam(value = "birthday")Date birthday
    ) {
        //接受参数
        User user = new User();
        //封装对象
        user.setUsername(username);
        user.setPassword(username);
        user.setAge(age);
        user.setPhone(phone);
        user.setEmail(email);
        user.setBirthday(birthday);

        //调用service保存
        System.out.println(user);



        return "success";
    }

    /**
     * 模拟注册场景
     * 很多参数 不怕 直接写对象就行了!!!!
     * 注意事项 属性名和参数名得一样
     *
     * @return
     */
    @RequestMapping("/test10")
    public String test10(User user) {


        //调用service保存
        System.out.println(user);



        return "success";
    }

    /**
     * 模拟注册场景
     * 参数直接写的对象 默认获取 参数名=参数值&参数值名=参数值~~~~~ 这个格式
     * @RequestBody 就是告诉springmvc框架 我数据在请求体中 而且是json格式的
     * @return
     */
    @RequestMapping("/test11")
    public String test11(@RequestBody User user) {


        //调用service保存
        System.out.println(user);



        return "success";
    }








}
